<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(!isset($is_webmaster))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Add block to template
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Add the block to list
		//-----------------------------------------------------------------

		if($template_mode == "2")
		{
			foreach($template_left_db as $idx=>$lefty)
				$last_left_block_order = $lefty['order'];

			$left_block_order = $last_left_block_order + 1;

			foreach($template_right_db as $idx=>$righty)
				$last_right_block_order = $righty['order'];

			$right_block_order = $last_right_block_order + 1;

			if($mode == "Left") $db->insertRow("template",array('mode' => 'left','name' => ''.$add_block.'','order' => ''.$left_block_order.''));
			if($mode == "Right") $db->insertRow("template",array('mode' => 'right','name' => ''.$add_block.'','order' => ''.$right_block_order.''));
		}
		else
		{
			foreach($template_left_db as $idx=>$lefty)
				$last_left_block_order = $lefty['order'];

			$left_block_order = $last_left_block_order + 1;

			$db->insertRow("template",array('mode' => 'left','name' => ''.$add_block.'','order' => ''.$left_block_order.''));
		}

		//-----------------------------------------------------------------
		//  Activate the block
		//-----------------------------------------------------------------

		$db->updateRow("blocks",array('used' => '1'),"name = '$add_block'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/layout&i=59");

	break;

	//-----------------------------------------------------------------
	//  Delete block from template
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Deactivate the block
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM template WHERE id = '$d'");
		$db->updateRow("blocks",array('used' => '0'),"name = '{$checker['name']}'");

		//-----------------------------------------------------------------
		//  Delete the block
		//-----------------------------------------------------------------

		$db->deleteRow("template","id = '$d'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/layout&i=60");

	break;

	//-----------------------------------------------------------------
	//  Save block order
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Save the order
		//-----------------------------------------------------------------

		foreach($order as $idx=>$val)
			$db->query("Update `template` set `order` = '$val' WHERE `id` = '$idx'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/layout&i=61");

	break;

	//-----------------------------------------------------------------
	//  Show list + form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Code for Left Block
		//-----------------------------------------------------------------

		if(!isset($leftblock)) $leftblock = NULL;

		if($template_left_db != FALSE)
		{
			foreach($template_left_db as $idx=>$lefty)
			{
				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$leftblock .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
					<td width='5%'><input type='text' name='order[{$lefty['id']}]' value='{$lefty['order']}' maxlength='2' class='box' style='width:100%'></td>
					<td width='90%'>{$lefty['name']}</td>
					<td width='5%'><a href='$PHP_SELF?n=modules/layout&a=2&d={$lefty['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Layout view left desc3']}'></a></td>
					</tr>";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$leftblock .= "
				<tr class='con1'>
				<td colspan='3'>{$language['Layout view left desc4']}</td>
				</tr>\n";
		}

		//-----------------------------------------------------------------
		//  Code for Right Block
		//-----------------------------------------------------------------

		if($template_mode == "2")
		{
			if(!isset($rightblock)) $rightblock = NULL;

			if($template_right_db != FALSE)
			{
				foreach($template_right_db as $idx=>$righty)
				{
					//-----------------------------------------------------------------
					//  Make Layout
					//-----------------------------------------------------------------

					$rightblock .= "
						<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
						<td width='5%'><input type='text' name='order[{$righty['id']}]' value='{$righty['order']}' maxlength='2' class='box' style='width:100%'></td>
						<td width='90%'>{$righty['name']}</td>
						<td width='5%'><a href='$PHP_SELF?n=modules/layout&a=2&d={$righty['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Layout view right desc3']}'></a></td>
						</tr>";
				}
			}
			else
			{
				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$rightblock .= "
					<tr class='con1'>
					<td colspan='3'>{$language['Layout view right desc4']}</td>
					</tr>\n";
			}
		}

		//-----------------------------------------------------------------
		//  Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Get blocks table 
		//-----------------------------------------------------------------

		$blocks_db_layout = $db->getTable("SELECT * FROM blocks WHERE used = '0'")

		?>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/layout">
			<table class="main" cellspacing="1" cellpadding="4">
			<? if($blocks_db_layout != NULL){ if($template_mode == "2") { ?><col width="50%"><? } else { ?><col width="80%"><? } if($template_mode == "2") { ?><col width="30%"><? } ?><col width="20%"><? } ?>
			<tr class="head"><td colspan="3"><?= $language['Layout add title'] ?></td></tr>
			<tr class="bottom"><td><?= $language['Layout add desc'] ?></td><? if($template_mode == "2" && $blocks_db_layout != NULL) { ?><td><?= $language['Layout add desc2'] ?></td><? } if($blocks_db_layout != NULL){ ?><td><?= $language['Layout add desc5'] ?></td><? } ?></tr>
			<tr><td class="con1"><? if($blocks_db_layout != NULL){ ?><select name="add_block" class="box" style="width:100%"><? foreach($blocks_db_layout as $blocks_layout) echo "<option>{$blocks_layout['name']}</option>" ?></select><? } else { echo $language['Layout add desc7']; } ?></td><? if($template_mode == "2" && $blocks_db_layout != NULL) { ?><td class="con1"><select name="mode" style="width:100%" class="box" tabindex="2"><option value="Left"><?= $language['Layout add desc3'] ?></option><option  value="Right"><?= $language['Layout add desc4'] ?></option></select></td><? } if($blocks_db_layout != NULL){ ?><td class="con1"><input type="submit" name="submit" style="width:100%" class="box" value="<?= $language['Layout add desc6'] ?>" tabindex="3"></td><? } ?></tr>
			<input type="hidden" name="a" value="1">
			</table></form>
			<br>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/layout">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<? if($template_mode == "2") { ?><col width="49%"><col width="2%"><col width="49%"><? } ?>
			<tr valign="top">
			<td><table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="3"><?= $language['Layout view left title'] ?></td></tr>
			<tr class="bottom"><td width="5%"><?= $language['Layout view left desc'] ?></td><td colspan="2" width="95%"><?= $language['Layout view left desc2'] ?></td></tr>
			<?= $leftblock ?>
			</table></td>
			<? if($template_mode == "2") { ?>
			<td>&nbsp;</td>
			<td><table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="3"><?= $language['Layout view right title'] ?></td></tr>
			<tr class="bottom"><td width="20%"><?= $language['Layout view right desc'] ?></td><td colspan="2" width="80%"><?= $language['Layout view right desc2'] ?></td></tr>
			<?= $rightblock ?>
			</table></td>
			</tr>
			<? } ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'>
			<td width="50%"><?= $language['Layout view overall desc'] ?></td>
			<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Layout view overall desc2'] ?>' tabindex='3'></td>
			</tr>
			<input type="hidden" name="a" value="3">
			</table></form>
		<?

	break;
}
?>